﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace PhysicsSimulator
{
    class CBird : CGameObject
    {
        private Vector2 _position = Vector2.Zero;
        public Vector2 Position
        {
            get
            {
                return _position;
            }
            set
            {
                _position = value;
            }
        }

        private Vector2 _birdPosition;
        public Vector2 BirdPosition
        {
            get
            {
                return _birdPosition;
            }
            set
            {
                _birdPosition = value;
            }
        }

        private CSprite _shaderTexture;
        public CSprite ShaderTexture
        {
            get
            {
                return _shaderTexture;
            }
            set
            {
                _shaderTexture = value;
            }
        }

        float _jumbLenght = 0.0f;
        float _ratio = 0.0f;
        float _anphal = 0.0f;

        private CSprite _birdTexture;
        public CSprite BirdTexture
        {
            get
            {
                return _birdTexture;
            }
            set
            {
                _birdTexture = value;
                
            }
        }

        public CBird(SpriteBatch spriteBatch) : base(spriteBatch)
        {

        }

        public override void Init(ContentManager contentManager)
        {
            base.Init(contentManager);
            Random rand = new Random(int.Parse(Guid.NewGuid().ToString().Substring(0, 8), System.Globalization.NumberStyles.HexNumber));
            _birdPosition = new Vector2(_position.X, _position.Y - _birdTexture.Height/2);
            _jumbLenght = _birdTexture.Height;
            _anphal = (float)(rand.NextDouble() * Math.PI * 2);
        }

        public override void Update(GameTimerEventArgs timer)
        {
            base.Update(timer);
            _anphal += 0.1f;
            if (_anphal > 2 * Math.PI)
                _anphal -= (float)(2 * Math.PI);

            _ratio = 0.75f + 0.25f * (float)(Math.Abs(Math.Cos(_anphal)));

            _birdPosition.Y = _position.Y - _birdTexture.Height / 2  + (- _ratio + 0.75f) * _jumbLenght;
        }

        public override void Draw(GameTimerEventArgs timer)
        {
            SpriteBatch.Draw(_shaderTexture.Texture,_position ,null,Color.White,0,_shaderTexture.Origin,_ratio,SpriteEffects.None,0);
            SpriteBatch.Draw(_birdTexture.Texture, _birdPosition , null, Color.White, 0, _birdTexture.Origin, 1, SpriteEffects.None, 0);
            base.Draw(timer);
        }
    }
}
